import { cloud } from '@/framework/utils/request'
import { zeroFill } from '@/utils/index.js'

/**
 * 处理图片回显方法
 */
export function handleImage(obj) {
  const res = [{
    id: obj.fileId,
    fileId: obj.fileId,
    name: obj.fileName || '',
    fileName: obj.fileName || '',
    url: `${location.origin}${process.env.VUE_APP_BASE_API}${cloud.file}/sysFileInfo/public/preview?fileId=${obj.fileId}`
  }]
  return res
}

/**
 * 处理附件回显方法
 */
export function handleFile(obj) {
  const res = {
    id: obj.fileId || obj.id,
    fileId: obj.fileId || obj.id,
    name: obj.fileName || obj.name,
    fileName: obj.fileName || obj.name || '',
    url: `${location.origin}${process.env.VUE_APP_BASE_API}${cloud.file}/sysFileInfo/publicDownload?fileId=${obj.fileId || obj.id}`
  }
  return res
}

/**
 * 将时间格式化成 `yyyy-MM-dd HH:mm:ss` 格式
 */
export function formatDate(dateStr) {
  if (!isNaN(Date.parse(dateStr))) {
    const dates = new Date(dateStr)
    const yyyy = dates.getFullYear()
    const MM = zeroFill(dates.getMonth() + 1)
    const dd = zeroFill(dates.getDate())
    const HH = zeroFill(dates.getHours())
    const mm = zeroFill(dates.getMinutes())
    const ss = zeroFill(dates.getSeconds())
    return `${yyyy}-${MM}-${dd} ${HH}:${mm}:${ss}`
  }
  return ''
}
